 var app = new Vue({
        el: '#app',
        data: {
            show_del: {
                status: false,
                text: '管理'
            },//是否显示删除面板
            goodsGroup: {}, // 商品集合
            isAllChecked: false,    // 是否全选
            balance: 0,  // 商品价格
        },
        methods: {
            deleteGoods: function () {   // 删除操作
                let cartids = this.findCurrentChecked();
                let _this = this;
                $.get(url + 'api/cart/delete', {cartid: cartids.toString(), uid:this.user.uid}, function (response) {
                    let responseData = JSON.parse(response);
                    if (responseData.code == '1') {
                        let goods = responseData.data;
                        if(goods.length > 0){
                            goods.forEach(function (itemGroup, index) {
                                itemGroup.goods.forEach(function (item, index) {
                                    item.checked = false;
                                });
                            });
                        }
                        _this.goodsGroup = goods;
                        mui.toast('删除成功');
                    }
                });
            },
            collectGoods: function () {   // 收藏
                let goodsid = this.findCurrentCheckedGoodid();
                $.post(url + 'api/Collect/addscollect', {goodsid: goodsid.toString(), uid:this.user.uid}, function (response) {
                    let responseData = JSON.parse(response);
                    if (responseData.code == '1') {
                        mui.toast('收藏成功');
                    }
                });
            },
            selectedCurrentGoods: function (cartid) { // 选中状态切换
                let goods = this.findGoodsByCartId(cartid);
                goods.checked = !goods.checked;
                this.countPrice();
            },
            operateGoods: function ({cartid: cartid, type: type}) { // 商品操作
                let goods = this.findGoodsByCartId(cartid);
                if (type == 0) {  //  减少操作
                    if(goods.num < 2){
                        return false;
                    }
                    $.post(url + 'api/cart/edit', {cartid: cartid, type: 'cut'}, function (response) {
                        let responseData = JSON.parse(response);
                        if (responseData.code == '1') {
                            --goods.num;
                        }
                    });
                } else {  // 增加操作
                    $.post(url + 'api/cart/edit', {cartid: cartid, type: 'plus'}, function (response) {
                        let responseData = JSON.parse(response);
                        if (responseData.code == '1') {
                            ++goods.num;
                        }
                    });
                }
                this.countPrice();
            },
            findGoodsByCartId: function (cartid) {  // 根据CartId:查询商品
                let goodsGroup = this.goodsGroup;
                let result = null;
                goodsGroup.forEach(function (itemGroup, index) {
                    itemGroup.goods.forEach(function (item, index) {
                        if (item.cartid == cartid) {
                            result = item;
                        }
                    });
                });
                return result;
            },
            countPrice: function () {   // 价格统计
                let goodsGroup = this.goodsGroup;
                let count = 0;
                goodsGroup.forEach(function (itemGroup, index) {
                    itemGroup.goods.forEach(function (item, index) {
                        if (item.checked) {
                            count += item.num * item.min_price;
                        }
                    });
                });
                this.balance = count;
            },
            changeManageModule: function () {    // 切换管理模块
                if (this.show_del.status) {
                    // 关闭
                    this.show_del.text = "管理"
                    this.show_del.status = false;
                } else {
                    // 打开
                    this.show_del.text = "完成"
                    this.show_del.status = true;
                }
            },
            selectAll: function () { // 全选
                let goodsGroup = this.goodsGroup;
                let isAllChecked = this.isAllChecked;
                if (isAllChecked) {
                    goodsGroup.forEach(function (itemGroup, index) {
                        itemGroup.goods.forEach(function (item, index) {
                            item.checked = false;
                        });
                    });
                    this.isAllChecked = false;
                } else {
                    goodsGroup.forEach(function (itemGroup, index) {
                        itemGroup.goods.forEach(function (item, index) {
                            item.checked = true;
                        });
                    });
                    this.isAllChecked = true;
                }
                this.countPrice();
            },
            findCurrentChecked: function () {   // 获取当前选中
                let goodsGroup = this.goodsGroup;
                let checkedArr = [];
                goodsGroup.forEach(function (itemGroup, index) {
                    itemGroup.goods.forEach(function (item, index) {
                        if (item.checked) {
                            checkedArr.push(item.cartid);
                        }
                    });
                });
                return checkedArr;
            },
            findCurrentCheckedGoodid: function () { // 获取goodids
                let goodsGroup = this.goodsGroup;
                let goodsids = [];
                goodsGroup.forEach(function (itemGroup, index) {
                    itemGroup.goods.forEach(function (item, index) {
                        if (item.checked) {
                            goodsids.push(item.goodsid);
                        }
                    });
                });
                return goodsids;
            },
            confirmOrder: function () { // 订单结算
                let checkCartids = this.findCurrentChecked();
                if(checkCartids.length <= 0){
                    mui.toast('请选择商品');
                    return false;
                }
                let cartInfo = {
                    cartids: checkCartids,
                    uid:this.user.uid,
                };
                host_config.localStorage.setItem(host_config.cartInfo,JSON.stringify(cartInfo));
                mui.openWindow('confirm_order.html?confirmType=0');
            }
        },
        mounted: function () {
            var _this = this;

            // 加载购物车数据
            let user = host_config.localStorage.user;
            if (user) {
                this.user = JSON.parse(user);
            }
            $.post(url + 'app/Cart/cartLists', {uid: this.user.uid}, function (response) {
                let responseData = JSON.parse(response);
                if (responseData.code == '1') {
                    if(responseData.data.length > 0){
                        let goods = responseData.data;
                        if(goods.length > 0){
                            goods.forEach(function (itemGroup, index) {
                                itemGroup.goods.forEach(function (item, index) {
                                    item.checked = false;
                                });
                            });
                        }
                        _this.goodsGroup = goods;
                    }else{
                        _this.goodsGroup = null;
                    }
                }
            })

        }
    })
    mui('.mui-bar-tab').on('tap', '.mui-tab-item', function () {
        console.log(this.href)
        mui.openWindow(this.href, this.href)
    })
